package com.fh.wanandroid.ui.knowledge.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fh.wanandroid.R;
import com.fh.wanandroid.app.Constants;
import com.fh.wanandroid.base.BaseFragment;
import com.fh.wanandroid.ui.knowledge.KnowledgeDataList;
import com.fh.wanandroid.ui.knowledge.activity.KnowledgeDetailActivity;
import com.fh.wanandroid.ui.knowledge.adapter.KnowledgeRvAdapter;
import com.fh.wanandroid.ui.knowledge.contract.KnowledgeContract;
import com.fh.wanandroid.ui.knowledge.presenter.KnowledgePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class KnowledgeFragment extends BaseFragment<KnowledgePresenter> implements KnowledgeContract.View, OnRefreshListener, OnLoadmoreListener, KnowledgeRvAdapter.ItemClickListener {

    @BindView(R.id.knowledge_recycler_view)
    RecyclerView knowledgeRecyclerView;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.drawer_layout)
    FrameLayout drawerLayout;


    ArrayList<KnowledgeDataList> knowledgeDataList;

    KnowledgeRvAdapter knowledgeRvAdapter;

    @Override
    public KnowledgePresenter getmPresenter() {
        return new KnowledgePresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null) {
            mPresenter.setView(this);
        }
    }

    @Override
    public int getLayoutId() {

        return R.layout.fragment_knowledge;
    }


    public static KnowledgeFragment getInstance(boolean param1, String param2) {

        Log.e("KnowledgeFragment", "--------------------1");
        KnowledgeFragment fragment = new KnowledgeFragment();
        Bundle args = new Bundle();
        args.putBoolean(Constants.ARG_PARAM1, param1);
        args.putString(Constants.ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {

        knowledgeRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        knowledgeRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        srl.setOnRefreshListener(this);
        srl.setOnLoadmoreListener(this);
    }


    public void jumpToTheTop() {
        if (knowledgeRecyclerView != null) {
            knowledgeRecyclerView.smoothScrollToPosition(0);
        }
    }


    @Override
    public void initData() {


        knowledgeDataList = new ArrayList<>();

        knowledgeRvAdapter = new KnowledgeRvAdapter(getContext(), knowledgeDataList);

        knowledgeRecyclerView.setAdapter(knowledgeRvAdapter);

        knowledgeRvAdapter.setmItemClickListener(this);


    }

    @Override
    public void loadData() {

        srl.autoRefresh();

    }

    @Override
    public void refreshKnowledge(List<KnowledgeDataList> knowledgeDataList) {


        this.knowledgeDataList.clear();

        this.knowledgeDataList.addAll(knowledgeDataList);

        knowledgeRvAdapter.notifyDataSetChanged();

        finishRefresh();

    }

    @Override
    public void appendKnowledge(List<KnowledgeDataList> knowledgeDataList) {

        finishLoadMore();

    }

    @Override
    public void loadDataFail(String msg) {

        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();

    }


    @Override
    public void onClick(int position) {

        Intent intent = new Intent(getContext(), KnowledgeDetailActivity.class);
        intent.putExtra("knowledgeDataList",knowledgeDataList.get(position));
        startActivity(intent);
//        knowledgeDataList

    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {

        mPresenter.loadMoreData();

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {

        mPresenter.loadInitData();

    }


    /**
     * * 结束刷新动作
     *
     * @return 是否结束刷新动作
     */
    private boolean finishRefresh() {
        if (srl.isRefreshing()) {
            srl.finishRefresh();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 结束加载更多动作
     */
    private void finishLoadMore() {
        if (srl.isLoading()) {
            srl.finishLoadmore();
        }
    }
}
